package test.y2023;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By  醉美柳舞之众星捧月
 *
 * @author song
 * @date 2023/10/19 16:29
 */
public class GR {

    class DataRow {
        private float JS; //井深
        private float CZJS; // 垂直井深
        private float JXJ; // 井斜角
        private String FWJ; // 方位角
        private float EZB; //未知字段
        private float NZB; //未知字段
        private float BHFW; //闭合方位
        private float BHJ; // 闭合距
        private String GTD; //未知字段 未用到
        private String SPWY; // 水平位移 未用到
        private float GR; // 伽马??
        private String FZ; //未知字段 未用到
        private String CLSJ; // 测量时间 未用到
    }

    public static void main(String[] args) {

        /* 实际 */
        List<DataRow> dt = new ArrayList<>();


        float xs, y1, y2;

        int[] GRDataLen = new int[1];
        //   float[,]GRData = new float[dt.size(), 4];
        float[][] GRData = new float[dt.size()][4];

        double interval = 0.2;
        float[] grAV = new float[1];
        float[] grMin = new float[1];

        GRDataAbstract(dt, interval, GRData, GRDataLen, grAV, grMin);

        float[] JXJ = new float[GRDataLen[0]];
        float cof = (float) 0.0;

        if (grAV[0] > 0) {
            cof = 1 / grAV[0];
        }

        int len = dt.size() - 1;
        float dx = dt.get(len).EZB;
        float dy = dt.get(len).NZB;
        //计算井方位角
        double alpha = 0;
        double ga = Math.atan(dy / dx);

        if (ga >= 0 && dx > 0) {
            alpha = 0.5 * Math.PI - ga;
            if (ga >= 0 && dx < 0) {
                alpha = 1.5 * Math.PI - ga;
            }
        }
        if (ga < 0 && dx > 0) {
            alpha = 0.5 * Math.PI + Math.abs(ga);
            if (ga < 0 && dx < 0) {
                alpha = 1.5 * Math.PI + Math.abs(ga);
            }
        }
        if (dx == 0 && ga > 0) {
            alpha = 0;
        }
        if (dx == 0 && ga < 0) {
            alpha = Math.PI;
        }


        for (int i = 0; i < GRDataLen[0]; i++) {
            for (int j = 0; j < dt.size() - 1; j++) {
                float a0 = GRData[i][2];

                //  float a1 = float.Parse(dt.Rows[j]["JS"].ToString());
                float a1 = dt.get(j).JS;
                //  float a2 = float.Parse(dt.Rows[j + 1]["JS"].ToString());
                float a2 = dt.get(j + 1).JS;

                /* TODO 取两个点中间的点;或者与后点重合 */
                if (a0 > a1 && a0 <= a2) {
                    xs = (a0 - a1) / (a2 - a1);
                    //  y1 = float.Parse(dt.Rows[j]["CZJS"].ToString());
                    y1 = dt.get(j).CZJS;
                    //  y2 = float.Parse(dt.Rows[j + 1]["CZJS"].ToString());
                    y2 = dt.get(j + 1).CZJS;

                    float yzb = xs * (y2 - y1) + y1;


                    //   y1 = float.Parse(dt.Rows[j]["JXJ"].ToString());
                    y1 = dt.get(j).JXJ;
                    //  y2 = float.Parse(dt.Rows[j + 1]["JXJ"].ToString());
                    y2 = dt.get(j + 1).JXJ;
                    float jxj = (xs * (y2 - y1) + y1);

                    //  y1 = float.Parse(dt.Rows[j]["BHJ"].ToString());
                    y1 = dt.get(j).BHJ;
                    //  y2 = float.Parse(dt.Rows[j + 1]["BHJ"].ToString());
                    y2 = dt.get(j + 1).BHJ;
                    float xzb = xs * (y2 - y1) + y1;

                    //  y1 = float.Parse(dt.Rows[j]["BHFW"].ToString());
                    y1 = dt.get(j).BHFW;
                    //  y2 = float.Parse(dt.Rows[j + 1]["BHFW"].ToString()); float bhfw = xs * (y2 - y1) + y1;
                    y2 = dt.get(j + 1).BHFW;

                    float bhfw = xs * (y2 - y1) + y1;

                    //  xzb = (float) (xzb * Math.Cos(bhfw / 180.0 * Math.PI - alpha));
                    xzb = (float) (xzb * Math.cos(bhfw / 180.0 * Math.PI - alpha));


                    GRData[i][0] = (float) (xzb + (cof * GRData[i][3] * 80 - cof * grMin[0] * 80) *
                            Math.sin((90 - jxj) / 180.0 * Math.PI));

                    GRData[i][1] = (float) (yzb - (cof * GRData[i][3] * 80 - cof * grMin[0] * 80) *
                            Math.cos((90 - jxj) / 180.0 * Math.PI));

                    //GRData[i, 0] = xzb ;
                    // GRData[i, 1] = yzb;
                    if (i > 1) {
                        if (GRData[i - 1][0] - GRData[i][0] > 5) {
                            GRData[i][0] = GRData[i - 1][0];
                        }
                    }
                    JXJ[i] = jxj;
                }
                if (a0 > a1 && a0 > a2 && j == dt.size() - 2) {
                    xs = (a0 - a1) / (a2 - a1);
                    //  y1 = float.Parse(dt.Rows[j]["CZJS"].ToString());
                    y1 = dt.get(j).CZJS;
                    //  y2 = float.Parse(dt.Rows[j + 1]["CZJS"].ToString());
                    y2 = dt.get(j + 1).CZJS;

                    float yzb = xs * (y2 - y1) + y1;


                    //  y1 = float.Parse(dt.Rows[j]["JXJ"].ToString()); y2 = float.Parse(dt.Rows[j + 1]["JXJ"].ToString());
                    y1 = dt.get(j).JXJ;
                    y2 = dt.get(j + 1).JXJ;
                    float jxj = (xs * (y2 - y1) + y1);

                    // y1 = float.Parse(dt.Rows[j]["BHJ"].ToString());
                    y1 = dt.get(j).BHJ;
                    // y2 = float.Parse(dt.Rows[j + 1]["BHJ"].ToString());
                    y2 = dt.get(j + 1).BHJ;

                    float xzb = xs * (y2 - y1) + y1;

                    //  y1 = float.Parse(dt.Rows[j]["BHFW"].ToString());
                    y1 = dt.get(j).BHFW;

                    // y2 = float.Parse(dt.Rows[j + 1]["BHFW"].ToString()); float bhfw = xs * (y2 - y1) + y1;
                    y2 = dt.get(j + 1).BHFW;
                    float bhfw = xs * (y2 - y1) + y1;

                    xzb = (float) (xzb * Math.cos(bhfw / 180.0 * Math.PI - alpha));

                    GRData[i][0] = (float) (xzb + (cof * GRData[i][3] * 80 - cof * grMin[0] * 80) *
                            Math.sin((90 - jxj) / 180.0 * Math.PI));
                    GRData[i][1] = (float) (yzb - (cof * GRData[i][3] * 80 - cof * grMin[0] * 80) *
                            Math.cos((90 - jxj) / 180.0 * Math.PI));

                    //GRData[i, 0] = xzb ;
                    // GRData[i, 1] = yzb;
                    if (i > 1) {
                        if (GRData[i][0] < GRData[i - 1][0]) {
                            GRData[i][0] = GRData[i - 1][0];
                        }
                    }
                    JXJ[i] = jxj;
                }
            }
        }
    }

    // GR数据抽稀
    public static void GRDataAbstract(List<DataRow> dt, double interval, float[][] GRData, int[] GRDataLen, float[] grAV, float[] grMin) {
        float fjs = 0;
        int fp = 0;
        int grc = 0;
        float sum = 0;
        for (int i = 0; i < dt.size(); i++) {
            fjs = dt.get(i).JS;
            fp = i;
            float val = dt.get(i).GR;

            if (fjs > 0 && val < 500) {
                GRData[0][2] = fjs;
                GRData[0][3] = dt.get(i).GR;
                sum = sum + GRData[0][3];
                grMin[0] = val;
                grc++;
                break;
            }
        }
        for (int j = fp + 1; j < dt.size(); j++) {
            float js = dt.get(j).JS;
            float val = dt.get(j).GR;

            if ((js - fjs) > interval && val < 500) {
                fjs = js;
                GRData[grc][2] = fjs;
                GRData[grc][3] = dt.get(j).GR;
                sum = sum + GRData[grc][3];
                if (val < grMin[0] && val > 0) {
                    grMin[0] = val;
                }
                grc++;
            }
        }
        GRDataLen[0] = grc;
        if (grc > 0) {
            grAV[0] = sum / grc;
        }
    }
}
